﻿Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports GameEx
Imports System.Windows.Forms


Namespace stigzler
    Public Class ReadGEInfo

        Private _GEPath As String = Nothing

        Property GEPath() As String
            Get
                Return _GEPath
            End Get
            Set(value As String)
                _GEPath = value
            End Set
        End Property

        Public Function GameExPath() As String
            Dim GEPath As String = (Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase))
            GEPath = GEPath.Remove(0, 6)
            GEPath = GEPath.Substring(0, GEPath.Length - 8)
            Return GEPath
        End Function

        Public Function GameExThemeMediaPath() As String
            Dim GEini As New IniFile
            GEini.Load(_GEPath & "\CONFIG\GameEx.ini")
            GameExThemeMediaPath = GEini.GetKeyValue("CachedImages3d", "themedir")

        End Function

        Public Function GEEmuList() As List(Of String)
            Dim EmuList As New List(Of String)
            Dim FinalEmu As String = ""
            'Iterate through files
            'db.echo(My.Settings.GEPath, False, "Path")
            Dim di As New DirectoryInfo(_GEPath & "\CONFIG\EMULATORS\IMPORT-EXPORT\")
            ' Get a reference to each file in that directory.
            Dim fiArr As FileInfo() = di.GetFiles()
            ' Display the names of the files.
            Dim fri As FileInfo
            Dim r As Regex = New Regex("(\(.*\))")

            For Each fri In fiArr

                Dim m As Match = r.Match(fri.Name)

                If (m.Success) Then
                    'db.echo(m.ToString.Trim(CChar("("), CChar(")")), False, "hit!")
                    FinalEmu = m.ToString.Trim(CChar("("), CChar(")"))

                    'EmuList.Add(FinalEmu)
                    If EmuList.Contains(FinalEmu) Then
                        'db.echo("Emu of this name already in list..")
                    Else
                        'db.echo("Added to list..")
                        EmuList.Add(FinalEmu)
                    End If

                End If
            Next fri
            Return EmuList
        End Function

        Public Function GESysList() As List(Of String)
            'db.echo(".")
            'db.echo("", False, "Systems Import")
            Dim SysList As New List(Of String)
            Dim FinalSys As String = ""
            'Iterate through files
            'db.echo(My.Settings.GEPath, False, "Path")
            Dim di As New DirectoryInfo(_GEPath & "\DATA\EMULATORS\")
            ' Get a reference to each file in that directory.
            Dim fiArr As FileInfo() = di.GetFiles()
            ' Display the names of the files.
            Dim fri As FileInfo
            Dim r As Regex = New Regex("\] ([A-Z].*)\.")

            For Each fri In fiArr

                Dim m As Match = r.Match(fri.Name)

                If (m.Success) Then
                    FinalSys = m.ToString.TrimStart({CChar("]"), CChar(" ")})
                    FinalSys = FinalSys.TrimEnd(CChar("."))


                    'db.echo("[" & FinalSys & "]", False, "hit!")
                    ' FinalSys = m.ToString.Trim(CChar("("), CChar(")"))

                    'EmuList.Add(FinalEmu)
                    If SysList.Contains(FinalSys) Then
                        'db.echo("Emu of this name already in list..")
                    Else
                        'db.echo("Added to list..")
                        SysList.Add(FinalSys)
                    End If

                End If
            Next fri

            Return SysList

        End Function


        Public Function GEUserEmus() As DataTable

            Dim emudt As New DataTable
            emudt.TableName = "UserEmulators"
            emudt.Columns.Add("EmuNumber", GetType(Integer))
            emudt.Columns.Add("StartPageName", GetType(String))
            emudt.Columns.Add("TitleText", GetType(String))
            emudt.Columns.Add("Database", GetType(String))
            emudt.Columns.Add("SnapPath", GetType(String))
            emudt.Columns.Add("TitlePath", GetType(String))
            emudt.Columns.Add("Enabled", GetType(Boolean))
            emudt.Columns.Add("RomPath", GetType(String))

            Dim GEini As New IniFile
            GEini.Load(_GEPath & "\CONFIG\GameEx.ini")
            For Each Section As String In GEini.SectionList
                If Section.Contains("Emulator_") Then
                    Dim EmuNumber As Integer = Integer.Parse(Regex.Replace(Section, "[^\d]", ""))
                    Dim StartPageName As String = GEini.GetKeyValue(Section, "STARTPAGENAME")
                    Dim TitleText As String = GEini.GetKeyValue(Section, "TITLETEXT")
                    Dim Database As String = GEini.GetKeyValue(Section, "DATABASE")
                    Dim SnapPath As String = GEini.GetKeyValue(Section, "SnapPath")
                    Dim TitlePath As String = GEini.GetKeyValue(Section, "TitlePath")
                    Dim Enabled As String = GEini.GetKeyValue(Section, "Enabled")
                    Dim RomPath As String = GEini.GetKeyValue(Section, "RomPath")

                    If StartPageName <> Nothing Then
                        ' MsgBox(EmuNumber & ", " & StartPageName & ", " & TitleText & ", " & Database)
                        emudt.Rows.Add(EmuNumber, StartPageName, TitleText, Database, SnapPath, TitlePath, Enabled, RomPath)
                    End If

                End If
            Next

            Return emudt
        End Function

        'Public Function ImportGEDb3(DBFileName As String, Optional SysID As Int32 = 0) As DataTable

        ''Application.DoEvents()



        ''Load DT with raw data:
        'Dim dt As New DataTable()
        'Try
        '    GEmvoy.db.echo("Data Source='" & _GEPath & "\DATA\EMULATORS\" & DBFileName & "'")
        '    Dim cnn As New SQLiteConnection("Data Source='" & _GEPath & "\DATA\EMULATORS\" & DBFileName & "'")
        '    cnn.Open()
        '    Dim mycommand As New SQLiteCommand(cnn)
        '    mycommand.CommandText = "Select * from GameData"
        '    Dim reader As SQLiteDataReader = mycommand.ExecuteReader()
        '    dt.Load(reader)
        '    reader.Close()
        '    cnn.Close()

        '    ' Catching exceptions is for communists
        'Catch e As Exception
        '    MsgBox("sql load error")
        '    GEmvoy.db.echo(e.Message, True)

        '    Return dt
        'End Try

        ''Process raw dt
        ''Dim count As Integer = 0
        ''GameSetMatch.OperationLB.Text = "Processing GameEx .db3 file...."
        ''GameSetMatch.PB1.Minimum = 0
        ''GameSetMatch.PB1.Maximum = dt.Rows.Count
        ''Application.DoEvents()

        'Dim newrow As DataRow

        'For Each row As DataRow In dt.Rows
        '    newrow = dt.NewRow
        '    newrow("Name") = row.Item("Name")
        '    newrow("SystemID") = SysID
        '    newrow("GameBase") = row.Item("GameBase")
        '    newrow("HyperList") = row.Item("HyperList")
        '    newrow("GoodName") = row.Item("GoodName")
        '    newrow("NoIntro") = row.Item("NoIntro")
        '    newrow("TOSEC") = row.Item("TOSEC")
        '    newrow("ReDump") = row.Item("ReDump")
        '    newrow("Publisher") = row.Item("Publisher")
        '    newrow("Developer") = row.Item("Developer")
        '    newrow("Date") = row.Item("Date")
        '    newrow("Catagory") = row.Item("Catagory")
        '    newrow("Genre") = row.Item("Genre")
        '    newrow("NumPlayers") = row.Item("NumPlayers")
        '    newrow("Description") = row.Item("Description")
        '    newrow("ESRB") = row.Item("ESRB")

        '    dt.Rows.Add(newrow)

        'Next

        'Return dt


        'End Function

    End Class
End Namespace


